package com.yetm.springbootdemo.service.impl;

import com.yetm.springbootdemo.entity.SMSCode;
import com.yetm.springbootdemo.service.SMSCodeService;
import com.yetm.springbootdemo.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    @Override
    //读|写缓存
    //@Cacheable(value = "smsCache", key = "#phone")
    //写入缓存
    @CachePut(value = "smsCache", key = "#phone")
    public String sendSMSCode(String phone) {
        String code = codeUtils.generateCode(phone);
        return code;
    }

    @Override
    public Boolean checkCode(SMSCode smsCode) {
        String code = smsCode.getCode();
        String cacheCode = codeUtils.getCacheCode(smsCode.getPhone());
        return code.equals(cacheCode);
    }
}
